package com.jgdt.demo.api;
import com.jgdt.demo.AppContext;
import com.librariy.json.JsonObject;
import com.librariy.net.HttpResponse;
public abstract class JsonResponse implements HttpResponse {
    public enum ResponseKey{success, code, message, data}
    public abstract void onCompleted(boolean success, int code, String message, JsonObject originObject);
    public void onCompleted(int responseCode, String responseText) {
        if(responseCode!=200){
            String errorMsg=parseStdError(responseCode,responseText);
            onCompleted(false,-1,errorMsg,null);
            return;
        }
        JsonObject resObj=new JsonObject(responseText);
        boolean success=resObj.optBoolean(ResponseKey.success,false);
        int code=resObj.optInt(ResponseKey.code,-1);
        String message=resObj.optString(ResponseKey.message,"");
        if(code==464){
            //AppContext.I.sendBroadcast(new Intent("com.bcia.assetmange.LOGIN"));
            //LoginWidget.reLogin(AppContext.I);
        }
        onCompleted(success,code,message,resObj);
    }
    public static String parseStdError(int responseCode, String responseText){
        if(responseCode<0){
            return responseText;
        }else if(responseCode!=200){
            return "["+responseCode+"]请求接口失败！";
        }
        return "["+responseCode+"]未知错误";
    }
}
